/*
 * SystemOutput.cpp
 *
 *  Created on: 10 Jun 2011
 *      Author: al2010
 */

#include "SystemOutput.h"

// C++ includes
#include <fstream>
#include <iostream>
#include <iomanip>

SystemOutput::SystemOutput() :
system(0)
{}
	
SystemOutput::SystemOutput(const System* system, string timeUnit)
{
	Set(system, timeUnit);
}
	
SystemOutput::~SystemOutput()
{}

void SystemOutput::Set(const System* system, string timeUnit)
{
	this->system = system;
	
	this->timeUnit = timeUnit;
}

void SystemOutput::Register(double time, const System* system)
{
	dataTime.push_back(time);
	
	dataMoles.push_back({});
	
	dataActivities.push_back({});
	
	for(unsigned iSpecies = 0; iSpecies < system->GetNumOfSpecies(); ++iSpecies)
		dataMoles.back().push_back(system->GetMolesOfSpecies(iSpecies));
	
	for(unsigned iSpecies = 0; iSpecies < system->GetNumOfSpecies(); ++iSpecies)
		dataActivities.back().push_back(system->GetActivityOfSpecies(iSpecies));
}

void SystemOutput::Print() const
{
	cout << setw(20) << "Time [" << timeUnit << "],";
	
	cout << setw(20) << "DeltaTime [" << timeUnit << "],";
	
	for(unsigned iSpecies = 0; iSpecies < system->GetNumOfSpecies(); ++iSpecies)
		cout << setw(20) << "M[" << system->GetSpecies(iSpecies).GetName() << "],";
	
	for(unsigned iSpecies = 0; iSpecies < system->GetNumOfSpecies(); ++iSpecies)
		cout << setw(20) << "A[" << system->GetSpecies(iSpecies).GetName() << "],";
	
	cout << endl;
	
	cout << scientific;
	
	for(unsigned iTime = 0; iTime < dataTime.size(); ++iTime)
	{
		cout << setw(20) << dataTime[iTime] << ",";
		
		cout << setw(20) << dataTime[iTime] - dataTime[((iTime == 0) ? 0 : iTime-1)] << ",";
		
		for(unsigned iSpecies = 0; iSpecies < dataMoles[iTime].size(); ++iSpecies)
			cout << setw(20) << dataMoles[iTime][iSpecies] << ",";
		
		for(unsigned iSpecies = 0; iSpecies < dataActivities[iTime].size(); ++iSpecies)
			cout << setw(20) << dataActivities[iTime][iSpecies] << ",";
		
		cout << endl;
	}
}

void SystemOutput::Print(string filename) const
{
	ofstream file(filename);
	
	file << "Time [" << timeUnit << "],";
	
	file << "DeltaTime [" << timeUnit << "],";
	
	for(unsigned iSpecies = 0; iSpecies < system->GetNumOfSpecies(); ++iSpecies)
		file << "M[" << system->GetSpecies(iSpecies).GetName() << "],";
	
	for(unsigned iSpecies = 0; iSpecies < system->GetNumOfSpecies(); ++iSpecies)
		file << "A[" << system->GetSpecies(iSpecies).GetName() << "],";
	
	file << endl;
	
	file << scientific;
	
	for(unsigned iTime = 0; iTime < dataTime.size(); ++iTime)
	{
		file << dataTime[iTime] << ",";
		
		file << dataTime[iTime] - dataTime[((iTime == 0) ? 0 : iTime-1)] << ",";
		
		for(unsigned iSpecies = 0; iSpecies < dataMoles[iTime].size(); ++iSpecies)
			file << dataMoles[iTime][iSpecies] << ",";
		
		for(unsigned iSpecies = 0; iSpecies < dataActivities[iTime].size(); ++iSpecies)
			file << dataActivities[iTime][iSpecies] << ",";
		
		file << endl;
	}
}
